﻿// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <windows.h>
#include "Shlwapi.h" // PathFileExistsA

#include "D:\iscdesk-sdk\huaweicloud-dme-iscdesk-cpp\IscdeskSdk\SdkApi\include\interface\SdkInvoker.h"
#include "D:\iscdesk-sdk\huaweicloud-dme-iscdesk-cpp\IscdeskSdk\SdkApi\include\sdk\IscdeskSdkIdl.h"

struct Example;
int getsimfolderTree(Example* example, std::string teamId);
int createFolderOnCloud(Example* example);
int uploadFileOnCloud(Example* example);
int getDownloadUrl(Example* example, std::string fileId);
int getSoftware(Example* example);
int getSoftwareRule(Example* example, std::string softwareId);
int getLicense(Example* example, std::string softwareId);
int getSoftwareQueue(Example* example, std::string softwareId);
int getSimeQueuePriority(Example* example, std::string queueId);
int cancelUpload(Example* example, std::string uploadId);
int submitTask(Example* example, SDK::SimSpaceTask& task);


std::atomic_bool running = true;

struct Example {
    void EventRecallFunc(SDK::RecallEvent e , std::string id) {
        std::cout << "\n\n\n";
        std::cout << "------------------------------------------------------";
        std::cout << "------------------------------------------------------";
        std::cout << "------------------------------------------\n";
        std::cout << "in event recall\n";

        std::cout << "\tevent: " << e << ", id: " << id << std::endl;

        switch (e) {
        case SDK::EVENT_HEARTBEAT_RESULT:
        {
            //心跳回复事件
            std::cout << "heartbeat" << std::endl;
            break;
        }
        case SDK::EVENT_OPENCLOUDFILE_RESULT:
        {
            //打开云端文件事件
            std::cout << "open cloud file result" << std::endl;
            break;
        }
        case SDK::EVENT_SAVE_RESULT:
        {
            //保存结果回复事件
            std::cout << "save result" << std::endl;
            break;
        }
        case SDK::EVENT_UPDATE:
        {
            //升级事件
            std::cout << "update" << std::endl;
            break;
        }
        case SDK::EVENT_UPGRADENOTICE:
        {
            //升级信息查询通知事件
            std::cout << "upgrade notice" << std::endl;
            break;
        }
        case SDK::EVENT_CLIENT_EXIT:
        {
            //iscdesk客户端关闭事件
            std::cout << "client exit" << std::endl;
            running = false;
            break;
        }
        case SDK::EVENT_LICENSE_VERIFY:
        {
            //license验证事件
            std::cout << "license verify" << std::endl;
            break;
        }
        case SDK::EVENT_SHORTCUT_SAVETOCLOUD:
        {
            //保存云端文件的快捷键事件
            std::cout << "shortcut save to cloud" << std::endl;
            break;
        }
        case SDK::EVENT_SHORTCUT_SAVEASTOCLOUD:
        {
            //另存为云端文件的快捷键事件
            std::cout << "shortcurt save as to cloud" << std::endl;
            break;
        }
        case SDK::EVENT_UPLOAD_START:
        {
            //文件上传云端开始事件
            std::cout << "upload start" << std::endl;
            break;
        }
        case SDK::EVENT_UPLOAD_END:
        {
            //文件上传云端结束事件
            std::cout << "upload end" << std::endl;
            break;
        }
        case SDK::EVENT_SIM_SOFTWARE_LIST:
        {
            //查询可用求解器列表
            std::cout << "sim software list" << std::endl;

            std::string softwareId = "1085413966459228160";
            getSoftwareRule(this, softwareId);

            break;
        }
        case SDK::EVENT_SIM_SOFTWARE_RULE:
        {
            //查询软件支持的文件后缀
            std::cout << "sim software rule" << std::endl;
            break;
        }
        case SDK::EVENT_SIM_SOFTWARE_LICENSE:
        {
            //查询软件的license信息
            std::cout << "sim software license" << std::endl;
            break;
        }
        case SDK::EVENT_SIM_RESOURCE_QUEUE:
        {
            //查询当前可用的资源队列
            std::cout << "sim resource queue" << std::endl;
            break;
        }
        case SDK::EVENT_SIM_QUEUE_PRIORITY:
        {
            //查询优先码
            std::cout << "sim queue priority" << std::endl;
            break;
        }
        case SDK::EVENT_SIM_UPLOAD_FILE:
        {
            //上传文件
            std::cout << "sim upload file" << std::endl;
            break;
        }
        case SDK::EVENT_SIM_UPLOAD_FILE_PROGRESS:
        {
            //分片上传文件进度
            std::cout << "sim upload file progress" << std::endl;
            break;
        }
        case SDK::EVENT_SIM_CACEL_UPLOAD:
        {
            //取消上传仿真文件
            std::cout << "sim cacel upload" << std::endl;
            break;
        }
        case SDK::EVENT_SIM_TASK:
        {
            //仿真作业提交
            std::cout << "sim task" << std::endl;
            break;
        }
        case SDK::EVENT_SIM_DATA_PAGE:
        {
            //分页查询数据列表
            std::cout << "sim data page" << std::endl;

            getsimfolderTree(this, teamId);

            {
                std::string fileId = "1087341004987531264";
                getDownloadUrl(this, fileId);
            }

            getSoftware(this);

            break;
        }
        case SDK::EVENT_SIM_DOWNLOAD_URL:
        {
            //获取仿真文件下载地址
            std::cout << "sim download url" << std::endl;
            break;
        }
        case SDK::EVENT_SIM_FOLDER_TREE:
        {
            //查询目录树
            std::cout << "sim folder tree" << std::endl;

            parentId = "1087338170365161472";
            uploadDir = "yunbosoft-folder10";

            createFolderOnCloud(this);

            break;
        }
        
        case SDK::EVENT_SIM_CREATE_FOLDER:
        {
            //新建文件夹
            std::cout << "sim create folder" << std::endl;

            parentId = "1087338170365161472";
            uploadDir = "yunbosoft-folder6";
            uploadFile = "D:\\iscdesk-sdk\\yunbosoft-file14.txt";

            uploadFileOnCloud(this);

            break;
        }
        case SDK::EVENT_UNKONW:
        {
            //未知事件
            std::cout << "unknown event, stop....." << std::endl;
            running = false;
            break;
        }
        default:
        {
            std::cout << "unknown event, stop...." << std::endl;
            running = false;
            break;
        }
        }

        std::cout << "===============================================================================";
        std::cout << "===============================================================================";
        std::cout << std::endl << std::endl;
    }

    std::string parentId = "1";
    std::string teamId = "1";
    std::string uploadDir;
    std::string uploadFile;
    SDK::IscdeskSdkIdl* iscdeskptr = nullptr;
};

int getsimfolderTree(Example* example, std::string teamId)
{
    /* 用户工业软件调用查询目录树 */
    SDK::ResultStatus resultStatus_38 = example->iscdeskptr->GetSimFolderTree(teamId);
    std::cout << "GetSimFolderTree result: " << resultStatus_38 << std::endl;

    return 0;
}

int createFolderOnCloud(Example* example)
{
    /* 用户工业软件调用创建仿真数据文件夹*/
    SDK::SimSpaceCreateData simSpaceCreateData;
    simSpaceCreateData.description = "yunbosoft-folder";
    simSpaceCreateData.filePath = example->uploadDir;
    simSpaceCreateData.name = example->uploadDir;
    simSpaceCreateData.parentId = example->parentId;
    simSpaceCreateData.teamId = example->teamId;

    SDK::ResultStatus resultStatus_47 = example->iscdeskptr->CreateSimFolder(simSpaceCreateData);
    std::cout << "CreateSimFolder result: " << resultStatus_47 << std::endl;

    return 0;
}

int uploadFileOnCloud(Example* example)
{
    /* 用户工业软件调用上传仿真数据文件*/
    SDK::SimSpaceCreateData simSpaceCreateData;
    simSpaceCreateData.description = "yunbosoft-file";
    simSpaceCreateData.filePath = example->uploadFile;
    simSpaceCreateData.name = example->uploadFile;
    simSpaceCreateData.parentId = example->parentId;
    simSpaceCreateData.teamId = example->teamId;

    SDK::ResultStatus resultStatus_48 = example->iscdeskptr->UploadSimFile(simSpaceCreateData);
    std::cout << "UploadSimFile result: " << resultStatus_48 << std::endl;

    return 0;
}

int getDownloadUrl(Example* example, std::string fileId)
{
    /* 用户工业软件调用获取仿真文件下载地址 */
    SDK::ResultStatus resultStatus_37 = example->iscdeskptr->GetSimFileDownloadUrl(fileId, example->teamId);
    std::cout << "GetSimFileDownloadUrl result: " << resultStatus_37 << std::endl;

    return 0;
}

int getSoftware(Example* example)
{
    /* 用户工业软件调用查询可用求解器列表*/
    SDK::ResultStatus resultStatus_42 = example->iscdeskptr->GetSimSoftwares();
    std::cout << "GetSimSoftwares result: " << resultStatus_42 << std::endl;

    return 0;
}

int getSoftwareRule(Example* example, std::string softwareId)
{
    /* 用户工业软件调用查询求解器支持的文件后缀*/
    SDK::ResultStatus resultStatus_43 = example->iscdeskptr->GetSoftwareRule(softwareId);
    std::cout << "GetSoftwareRule result: " << resultStatus_43 << std::endl;

    return 0;
}

int getLicense(Example* example, std::string softwareId)
{
    /* 用户工业软件调用查询求解器 license 信息*/
    SDK::ResultStatus resultStatus_44 = example->iscdeskptr->GetSoftwareLicense(softwareId);
    std::cout << "GetSoftwareLicense result: " << resultStatus_44 << std::endl;

    return 0;
}

int getSoftwareQueue(Example* example, std::string softwareId)
{
    /* 用户工业软件调用查询当前可用的资源队列*/
    SDK::ResultStatus resultStatus_45 = example->iscdeskptr->GetSimResourceQueue(softwareId);
    std::cout << "GetSimResourceQueue result: " << resultStatus_45 << std::endl;

    return 0;
}

int getSimeQueuePriority(Example* example, std::string queueId)
{
    /* 用户工业软件调用查询队列优先码*/
    SDK::ResultStatus resultStatus_46 = example->iscdeskptr->GetSimQueuePriorityCode(queueId);
    std::cout << "GetSimQueuePriorityCode result: " << resultStatus_46 << std::endl;

    return 0;
}

int cancelUpload(Example* example, std::string uploadId)
{
    /* 用户工业软件调用取消上传仿真文件*/
    SDK::ResultStatus resultStatus_49 = example->iscdeskptr->CancelUploadSimFile(uploadId);
    std::cout << "CancelUploadSimFile result: " << resultStatus_49 << std::endl;

    return 0;
}

int submitTask(Example* example, SDK::SimSpaceTask& task)
{
    /* 用户工业软件调用提交仿真作业*/
    SDK::ResultStatus resultStatus_410 = example->iscdeskptr->SubmitSimSpaceTask(task);
    std::cout << "SubmitSimSpaceTask result: " << resultStatus_410 << std::endl;

    return 0;
}

int main()
{
    Example example;

    std::cout << "Hello World!\n";

    char path[MAX_PATH];
    GetModuleFileNameA(NULL, path, MAX_PATH);

    std::cout << "当前工作路径： " << path << std::endl;

    std::string sdkdllpath = "D:\\iscdesk-sdk\\huaweicloud-dme-iscdesk-cpp\\IscdeskSdk\\SdkApi\\bin\\x64\\Release\\IscdeskApi64.dll";

    if (PathFileExistsA(sdkdllpath.c_str()) == 1) {
        std::cout << "file " << sdkdllpath << " existed\n";
    }
    else {
        std::cout << "file " << sdkdllpath << " invalid\n";
        return -1;
    }

    HMODULE sdkHandle = LoadLibraryA(sdkdllpath.c_str());
    if (sdkHandle == NULL) {
        std::cout << "Load " << sdkdllpath << " fail" << std::endl;
        return -1;
    }
    else {
        std::cout << " load  " << sdkdllpath << " success\n";
    }

    SDK::SdkInvoker invoker(sdkHandle);
    SDK::ResultStatus ret = invoker.IscdeskSdkCheckingEnvironment();
    if (ret != SDK::STATUS_SUCCESS) {
        std::cout << "checking env failed, " << ret << "\n";
        return -2;
    }
    else {
        std::cout << "check env success\n";
    }
    SDK::IscdeskSdkIdl* iscdesk = invoker.IscdeskFactory();
    ret = iscdesk->Init();
    if (ret != SDK::ResultStatus::STATUS_SUCCESS) {
        std::cout << "factory init failed\n";
        return -3;
    }
    else {
        std::cout << "factory init success\n";
    }

    example.iscdeskptr = iscdesk;

    SDK::ResultStatus resutStatus_1 = iscdesk->Init();

    iscdesk->RegisterEventRecall(&example, &Example::EventRecallFunc);

    /* 用户工业软件调用分页查询仿真数据列表 */
    SDK::SimSpaceData simSpaceData;
    SDK::ResultStatus resultStatus_36 = iscdesk->PageQuerySimSpaceData(simSpaceData);
    std::cout << "PageQuerySimSpaceData result: " << resultStatus_36 << std::endl;


    while (running)
    {
        Sleep(1000);
        std::cout << "按下回车继续..." << std::endl;
        std::cin.get();

        running = false;
    }

    iscdesk->ShutOut();
    std::cout << "\n\n----------------------------------------------------------------------------------------------------------\n";
    Sleep(1 * 1000);

    exit(0);

}

